
/*
 * Copyright 2002-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2021原作者。
 *
 *根据Apache许可证2.0版（“许可证”）许可；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，否则软件
 *根据许可证分发是在“按原样”的基础上分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证
 *许可证下的限制。
 */

package org.springframework.format.support;

import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.CurrencyUnitFormatter;
import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.money.MonetaryAmountFormatter;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringValueResolver;

/**
 * A specialization of {@link FormattingConversionService} configured by default with
 * converters and formatters appropriate for most applications.
 *
 * <p>Designed for direct instantiation but also exposes the static {@link #addDefaultFormatters}
 * utility method for ad hoc use against any {@code FormatterRegistry} instance, just
 * as {@code DefaultConversionService} exposes its own
 * {@link DefaultConversionService#addDefaultConverters addDefaultConverters} method.
 *
 * <p>Automatically registers formatters for JSR-354 Money &amp; Currency, JSR-310 Date-Time
 * and/or Joda-Time 2.x, depending on the presence of the corresponding API on the classpath.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.1
 */
/**
 *｛@link FormattingConversionService｝的专用化，默认情况下配置为
 *适用于大多数应用程序的转换器和格式化程序。
 *
 *＜p＞设计用于直接实例化，但也公开静态｛@link#addDefaultFormatters｝
 *针对任何｛@code FormatterRegistry｝实例临时使用的实用方法，仅
 *因为｛@code DefaultConversionService｝公开了它自己的
 *{@link DefaultConversionService#addDefaultConverters addDefaultConverters}方法。
 *
 *<p>自动注册JSR-354 Money&amp；货币，JSR-310日期时间
 *和/或Joda-Time 2.x，具体取决于类路径上是否存在相应的API。
 *
 *@作者Chris Beams
 *@作者Juergen Hoeller
 *@自3.1起
 */
public class DefaultFormattingConversionService extends FormattingConversionService {

	private static final boolean jsr354Present;

	static {
		ClassLoader classLoader = DefaultFormattingConversionService.class.getClassLoader();
		jsr354Present = ClassUtils.isPresent("javax.money.MonetaryAmount", classLoader);
	}

	/**
	 * Create a new {@code DefaultFormattingConversionService} with the set of
	 * {@linkplain DefaultConversionService#addDefaultConverters default converters} and
	 * {@linkplain #addDefaultFormatters default formatters}.
	 */
	/**
	 *使用
	 *｛@linkplain DefaultConversionService#addDefaultConverters默认转换器｝和
	 *｛@linkplain#addDefaultFormatters默认格式化程序｝。
	 */
	public DefaultFormattingConversionService() {
		this(null, true);
	}

	/**
	 * Create a new {@code DefaultFormattingConversionService} with the set of
	 * {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
	 * based on the value of {@code registerDefaultFormatters}, the set of
	 * {@linkplain #addDefaultFormatters default formatters}.
	 * @param registerDefaultFormatters whether to register default formatters
	 */
	/**
	 *使用
	 *{@linkplain DefaultConversionService#addDefaultConverters默认转换器}和，
	 *基于{@code registerDefaultFormatters}的值
	 *｛@linkplain#addDefaultFormatters默认格式化程序｝。
	 *@param registerDefaultFormatters是否注册默认格式化程序
	 */
	public DefaultFormattingConversionService(boolean registerDefaultFormatters) {
		this(null, registerDefaultFormatters);
	}

	/**
	 * Create a new {@code DefaultFormattingConversionService} with the set of
	 * {@linkplain DefaultConversionService#addDefaultConverters default converters} and,
	 * based on the value of {@code registerDefaultFormatters}, the set of
	 * {@linkplain #addDefaultFormatters default formatters}.
	 * @param embeddedValueResolver delegated to {@link #setEmbeddedValueResolver(StringValueResolver)}
	 * prior to calling {@link #addDefaultFormatters}.
	 * @param registerDefaultFormatters whether to register default formatters
	 */
	/**
	 *使用
	 *{@linkplain DefaultConversionService#addDefaultConverters默认转换器}和，
	 *基于{@code registerDefaultFormatters}的值
	 *｛@linkplain#addDefaultFormatters默认格式化程序｝。
	 *@param embeddedValueResolver委托给｛@link#setEmbeddedValueResolver（StringValueResolver）｝
	 *在调用｛@link#addDefaultFormatters｝之前。
	 *@param registerDefaultFormatters是否注册默认格式化程序
	 */
	public DefaultFormattingConversionService(
			@Nullable StringValueResolver embeddedValueResolver, boolean registerDefaultFormatters) {

		if (embeddedValueResolver != null) {
			setEmbeddedValueResolver(embeddedValueResolver);
		}
		DefaultConversionService.addDefaultConverters(this);
		if (registerDefaultFormatters) {
			addDefaultFormatters(this);
		}
	}


	/**
	 * Add formatters appropriate for most environments: including number formatters,
	 * JSR-354 Money &amp; Currency formatters, JSR-310 Date-Time and/or Joda-Time formatters,
	 * depending on the presence of the corresponding API on the classpath.
	 * @param formatterRegistry the service to register default formatters with
	 */
	/**
	 *添加适用于大多数环境的格式化程序：包括数字格式化程序，
	 *JSR-354货币&amp；货币格式器、JSR-310日期时间和/或Joda时间格式器，
	 *取决于类路径上是否存在相应的API。
	 *@param formatter注册用于注册默认格式化程序的服务
	 */
	public static void addDefaultFormatters(FormatterRegistry formatterRegistry) {
		// Default handling of number values
		// 数值的默认处理
		formatterRegistry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());

		// Default handling of monetary values
		// 货币价值的默认处理
		if (jsr354Present) {
			formatterRegistry.addFormatter(new CurrencyUnitFormatter());
			formatterRegistry.addFormatter(new MonetaryAmountFormatter());
			formatterRegistry.addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory());
		}

		// Default handling of date-time values
		// 日期时间值的默认处理

		// just handling JSR-310 specific date and time types
		// 只处理JSR-310特定的日期和时间类型
		new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry);

		// regular DateFormat-based Date, Calendar, Long converters
		// 常规日期基于格式的日期、日历、长转换器
		new DateFormatterRegistrar().registerFormatters(formatterRegistry);
	}

}
